﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TTServer.Models;


namespace TTServer.Controllers
{
    public class TSController : ApiController
    {
        private ApiTools tool = new ApiTools();
        [HttpGet]
        public HttpResponseMessage NowTime()
        {
            return tool.MsgFormat(GetHttpWebRequest());
        }

        private string GetHttpWebRequest()
        {
            try
            {
                //Uri uri = new Uri("https://f.m.suning.com/api/ct.do");  //苏宁时间同步api
                Uri uri = new Uri("http://api.pinduoduo.com/api/server/_stm");  //拼夕夕时间同步api  
                HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
                myReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3100.0 Safari/537.36";
                myReq.Accept = "*/*";
                myReq.KeepAlive = true;
                myReq.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
                HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
                Stream receviceStream = result.GetResponseStream();
                StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding("utf-8"));
                string strHTML = readerOfStream.ReadToEnd();
                readerOfStream.Close();
                receviceStream.Close();
                result.Close();
                return strHTML;
            }
            catch
            {
                return DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss");
            }
        }


    }
}
